package com.wtb.javatool.service.impl;

import com.wtb.javatool.dao.TableMapper;
import com.wtb.javatool.entity.Dish;
import com.wtb.javatool.entity.Table;
import com.wtb.javatool.service.ITableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TableService implements ITableService {
    @Autowired
    private TableMapper tableMapper;

    /**
     * 列出所有桌位
     * @return
     */
    public List<Table> listAllTables(){
        return tableMapper.listAllTables();
    }

    /**
     * 获取桌位预定信息
     *
     * @param tableId 桌位ID
     * @return
     */
    @Override
    public List<Object> getTableReservations(Long tableId) {
        return tableMapper.getTableReservations(tableId);
    }

    /**
     * 添加桌位预定
     *
     * @param tableId 桌位ID
     * @param dishId 菜品ID
     * @param quantity 数量
     * @return
     */
    @Override
    public boolean addTableReservation(Long tableId, Long dishId, Integer quantity) {
        try {
            int result = tableMapper.addTableReservation(tableId, dishId, quantity);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除桌位预定
     *
     * @param reservationId 预定ID
     * @return
     */
    @Override
    public boolean deleteTableReservation(Long reservationId) {
        try {
            int result = tableMapper.deleteTableReservation(reservationId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更新桌位状态
     *
     * @param tableId 桌位ID
     * @param status 状态
     * @return
     */
    @Override
    public boolean updateTableStatus(Long tableId, Integer status) {
        try {
            int result = tableMapper.updateTableStatus(tableId, status);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 预定桌位
     *
     * @param tableId 桌位ID
     * @param customerName 客户姓名
     * @param customerPhone 客户电话
     * @return
     */
    @Override
    public boolean reserveTable(Long tableId, String customerName, String customerPhone) {
        try {
            int result = tableMapper.reserveTable(tableId, customerName, customerPhone);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 结束预定
     *
     * @param tableId 桌位ID
     * @return
     */
    @Override
    public boolean endReservation(Long tableId) {
        try {
            System.out.println("=== 开始结束预定操作 ===");
            System.out.println("桌位ID: " + tableId);

            // 1. 清空桌位信息（状态、客户信息）
            int tableResult = tableMapper.endReservation(tableId);
            System.out.println("桌位更新结果: " + tableResult);

            // 2. 删除该桌位的所有预定信息
            int bookResult = tableMapper.deleteTableBookByTableId(tableId);
            System.out.println("预定删除结果: " + bookResult);

            // 只要桌位更新成功就认为操作成功
            boolean result = tableResult > 0;
            System.out.println("结束预定操作结果: " + result);
            System.out.println("=== 结束预定操作完成 ===");

            return result;
        } catch (Exception e) {
            System.out.println("结束预定操作异常: " + e.getMessage());
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 执行预定
     *
     * @param tableId 桌位ID
     * @return
     */
    @Override
    public boolean executeReservation(Long tableId) {
        try {
            int result = tableMapper.executeReservation(tableId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更新预定状态
     *
     * @param reservationId 预定ID
     * @param status 状态
     * @return
     */
    @Override
    public boolean updateReservationStatus(Long reservationId, Integer status) {
        try {
            int result = tableMapper.updateReservationStatus(reservationId, status);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
